var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLoading: true,
        navHeight: app.globalData.navHeight, //导航栏高度
        topOpacity: 0,

        is_vip: 0,
        is_shop: 0,
        text: '',

        goodList: [],
        shareItem: {}
    },

    onPageScroll: function (e) {
        if (e.scrollTop > 50) {
            let opacity = e.scrollTop / 300;
            opacity = opacity > 1 ? 1 : opacity;
            this.setData({
                topOpacity: opacity
            });
        } else {
            this.setData({
                topOpacity: 0
            });
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //用户授权登录
        this.setData({
            text: options.text || ''
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var that = this;
        wx.showLoading({
            title: "加载中"
        })
        app.login(function (userInfo) {
            that.setData({
                userInfo: userInfo
            });

            that.getUserInfo()
            that.getGoodListApi()
        });
    },

    getUserInfo: function () {
        var that = this;
        var paraArr = new Array();
        paraArr['id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api_user/' + paraArr['id'],
            data: {
                sign: sign
            },
            method: 'GET',
            header: {},
            success: function (res) {
                that.setData({
                    is_vip: res.data.data.is_vip,
                    is_shop: res.data.data.is_shop,
                })
            }
        })
    },

    getInfoApi: function () {
        var that = this;
        var paraArr = new Array();
        wx.request({
            url: rootDocment + '/api/goods/pianhao',
            data: {},
            method: 'GET',
            success: function (res) {
                console.log(res.data.data)

            }
        })
    },

    getGoodListApi: function () {
        var that = this;
        var paraArr = new Array();
        paraArr['size'] = "20";
        paraArr['type'] = "pianhao";
        paraArr['user_id'] = that.data.userInfo.userID;
        paraArr['pianhao'] = that.data.text;
        console.log(paraArr)
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api_goods',
            data: {
                user_id: paraArr['user_id'],
                size: paraArr['size'],
                type: paraArr['type'],
                pianhao: paraArr['pianhao'],
                sign: sign
            },
            method: 'GET',
            header: {
                "Content-Type": "application/json;charset=utf-8"
            },
            success: function (res) {
                console.log(res.data.data)
                that.setData({
                    goodList: res.data.data
                });
            },
            complete: () => {
                wx.hideLoading()
                that.setData({
                    isLoading: false
                })
            }
        })
    },

    shareBtn(e) {
        const {
            item
        } = e.currentTarget.dataset

        this.setData({
            shareItem: item
        })
    },

    goDetailBtn(e) {
        let {
            id
        } = e.currentTarget.dataset
        wx.navigateTo({
            url: `/pages/goods/detail?id=${id}`,
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        console.log(res);
        if (res.from == "button") {
            var that = this;
            const item = res.target.dataset.item

            return {
                title: item.title,
                path: '/pages/goods/detail?scene=' + that.data.userInfo.userID + '_' + item.id,
                imageUrl: item.pic
            }
        }

    },
})